package com.zx._01_基础._018_注解;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ProcessMyAnnotation {

    public static void process(String classFullName) throws SecurityException, ClassNotFoundException {
        Class<?> clazz = Class.forName(classFullName);//根据全限定路径获得这个类的反射
        Method[] methods = clazz.getMethods();//获得这个类的所有方法
        for (Method m : methods) {
            //查看classFullName这个类里面哪个方法被MyAnnotation注解修饰
            if (m.isAnnotationPresent(MyAnnotation.class)) {
                try {
                    //m.invoke(null);//调用m方法
                    //获得这个方法的所有注解
                    Annotation[] as = m.getAnnotations();
                    for (Annotation ann : as) {
                        //如果ann是MyAnnotation类型的，则强制转换
                        if (ann instanceof MyAnnotation) {
                            int id = ((MyAnnotation) ann).id();
                            String name = ((MyAnnotation) ann).name();
                            System.out.println("id=" + id + ",name=" + name);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
